package GUI.Cocina;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.Timer;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Button;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import sale.entities.OrderVO;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.sale.interfaces.SaleProxyMgt;

public class VerOrdenesAbiertas extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public VerOrdenesAbiertas(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(662, 411);
		shell.setText(getText());
		shell.setLayout(null);
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(10, 10, 532, 255);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(100);
		tblclmnNewColumn.setText("Orden");
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(100);
		tblclmnNewColumn_1.setText("Articulo");
		
		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setWidth(169);
		tblclmnNewColumn_2.setText("Especificacion");
		
		TableColumn tblclmnNewColumn_3 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_3.setWidth(161);
		tblclmnNewColumn_3.setText("Estado");
		
		Button btnMarcarComoListo = new Button(shell, SWT.NONE);
		btnMarcarComoListo.setBounds(255, 298, 133, 28);
		btnMarcarComoListo.setText("Marcar Como Listo");
		
		Button btnRechazarOrden = new Button(shell, SWT.NONE);
		btnRechazarOrden.setBounds(255, 328, 133, 28);
		btnRechazarOrden.setText("Rechazar Orden");
		  int delay = 1000; //milliseconds
		  ActionListener taskPerformer = new ActionListener() {
		      public void actionPerformed(ActionEvent evt) {
		    	  SaleProxyMgt uso = ServiceFacade.getInstance().getSaleProxyMgt();
		  		OrderVO[] sales=null;
		  		try {
		  			sales = uso.getOpenOrders();
		  		} catch (RemoteException e) {
		  			ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
		  		//	e.printStackTrace();
		  		} catch (ProxyException e) {
		  			ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
		  			//e.printStackTrace();
		  		}
		  		
		  		for(int i=0;i<sales.length;i++){
		  			
		  				for (int j = 0; j< sales[i].getArts().size();j++){
		  					
		  					TableItem tableItem = new TableItem(table, SWT.NONE);
		  					tableItem.setText(1, sales[i].getArts().get(j).getName());
		  					tableItem.setText(2,sales[i].getEspecif().get(j));
		  					int m = sales[i].getTableKey();
		  					if (m==-1){
		  						tableItem.setText(0, "Mostrador");
		  						
		  					}
		  					if(m==-2){
		  						tableItem.setText(0,"Delivery");
		  					}
		  					if (m>=0){
		  						tableItem.setText(0,String.valueOf(m));
		  					}
		  					
		  					tableItem.setText(3, "Esperando coccion");
		  				}
		  			}
		      
		      }

		
		  };
		  new Timer(delay, taskPerformer).start();
		//This method schedules the specified task for repeated fixed-rate execution, beginning at the specified time.
		SaleProxyMgt uso = ServiceFacade.getInstance().getSaleProxyMgt();
		OrderVO[] sales=null;
		try {
			sales = uso.getOpenOrders();
		} catch (RemoteException e) {
			ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
			ventana.open();
			shell.dispose();
			e.printStackTrace();
		} catch (ProxyException e) {
			ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
			ventana.open();
			shell.dispose();
			e.printStackTrace();
		}
		
		for(int i=0;i<sales.length;i++){
			
				for (int j = 0; j< sales[i].getArts().size();j++){
					
					TableItem tableItem = new TableItem(table, SWT.NONE);
					tableItem.setText(1, sales[i].getArts().get(j).getName());
					tableItem.setText(2,sales[i].getEspecif().get(j));
					int m = sales[i].getTableKey();
					if (m==-1){
						tableItem.setText(0, "Mostrador");
						
					}
					if(m==-2){
						tableItem.setText(0,"Delivery");
					}
					if (m>=0){
						tableItem.setText(0,String.valueOf(m));
					}
					
					tableItem.setText(3, "Esperando coccion");
				}
			}
			
		
			
		
		
	}
}
